let db = require("../db/index");

// 查询储藏室列表
exports.storeroomlist = (req, res) => {
    var sql =
        `select * from building,community,storeroom WHERE storeroom.communityID=community.communityID AND storeroom.BuildingID=building.BuildingID`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
            // console.log(data)
        }
    });
};

// 修改储藏室列表数据
exports.storeroomlistuodate = (req, res) => {
    console.log(req.body)
    var sql =
        `UPDATE storeroom set FoolNumber="${req.body.FoolNumber}",StoreCode=${req.body.StoreCode},CreateArea="${req.body.CreateArea}",UsageArea="${req.body.UsageArea}",Housingstatus="${req.body.Housingstatus}",communityID=${req.body.communityID},BuildingID=${req.body.BuildingID} WHERE date=${req.body.date}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 新增储藏室列表数据
exports.storeroomlistinsert = (req, res) => {
    console.log(req.body)
    var sql =
        `SELECT communityID FROM community WHERE communityName="${req.body.communityName}"`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT communityID,BuildingID FROM building WHERE communityID=${data[0].communityID} and BuildingName="${req.body.BuildingName}"`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    console.log(data[0].communityID,data[0].BuildingID)
                    var sql =
                        `INSERT INTO storeroom(FoolNumber,StoreCode,CreateArea,UsageArea,Housingstatus,communityID,BuildingID,StoreRoomNote,Housetype,edit) VALUES("${req.body.FoolNumber}",${req.body.StoreCode},"${req.body.CreateArea}","${req.body.UsageArea}","${req.body.Housingstatus}",${data[0].communityID},${data[0].BuildingID},"${req.body.StoreRoomNote}","储藏室","绑定业主")`;
                    db.query(sql, [], (err, data) => {
                        if (err) {
                            console.log("出错了", err);
                            return res.send("错误" + err.message);
                        }else{
                            res.send({
                                data,
                                state:200
                            });
                        }
                    });
                }
            });
        }
    });
};
// 删除储藏室列表数据
exports.storeroomlisdelate = (req, res) => {
    console.log(req.body)
    var sql =
        `DELETE FROM storeroom WHERE date=${req.body.StoreRoomID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from building,community,storeroom WHERE storeroom.communityID=community.communityID AND storeroom.BuildingID=building.BuildingID`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};

// 按需查找储藏室信息
exports.storeroomselect = (req, res) => {
    var sql =
        `SELECT * FROM storeroom WHERE owner_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};